package com.lbs.droiddoctor.disease;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.droiddoctor.R;
import com.lbs.droiddoctor.base.BaseActivity;
import com.lbs.droiddoctor.common.HttpASync;
import com.lbs.droiddoctor.common.HttpASync.OnRequest;
import com.lbs.droiddoctor.constants.AppConstants;
import com.lbs.view.adapter.DiseaseAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class SearchActivity extends BaseActivity implements OnClickListener,OnRequest{
	private ListView diseaseList;
	private DiseaseAdapter adapter;
	private ImageView imageView1;
	private ArrayList<Disease> diseaseListData;
	private Context ctx;
	private EditText key;
	private Button button;
	private Handler uiHandler = new Handler() {
		
		  @Override
		public void handleMessage(Message msg) {
			  if(msg.what == 1){
				  int position = msg.arg1;
					String disease = diseaseListData.get(position).getDiseaseName();
					String diseasePathUrl = AppConstants.DELETE_DISEASE_PATH + "&disease="+disease;
					HttpASync request = new HttpASync();					
					request.execute(diseasePathUrl);
				  diseaseListData.remove(position);
				  runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						adapter.notifyDataSetChanged();
						
					}
				});
				  
			  }else  if(msg.what == 2){
				  final int position = msg.arg1;
				
				  runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						
						Intent intent = new Intent(ctx,AddDisease.class);
						intent.putExtra("Disease", diseaseListData.get(position));
						ctx.startActivity(intent);
					}
				});
				  
			  }
			
		}
	};
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.search);
	this.ctx = this;
	System.out.println("SearchActivity.onCreate()");
	diseaseList = (ListView) findViewById(R.id.listView1);
	//imageView1 = (ImageView) findViewById(R.id.search_icon);
	key=(EditText) findViewById(R.id.searchtext);
	button = (Button)findViewById(R.id.button1);
	//imageView1.setFocusable(true);
	/////imageView1.setOnClickListener(this);
	button.setOnClickListener(this);
	
}
@Override
public void onClick(View v) {
	System.out.println("SearchActivity.onClick()");
	if(v.getId()==R.id.button1){
		String searchKey=key.getEditableText().toString();
		if(!searchKey.equals("")) {
			String loginUrl = AppConstants.DISEASE_PATH+"?method=search&key="+searchKey;
			HttpASync request  = new HttpASync();
			request.setOnRequestListener(this);
			request.execute(loginUrl);
			showProgress("Searching ... Please Wait...");
		} else {
			Toast.makeText(this, "Enter something to search", Toast.LENGTH_SHORT).show();
			
		}
	}
	
}
@Override
public void onResponseReceived(String response) {
	Gson gson = new Gson();
	
	 diseaseListData = gson.fromJson(response,new TypeToken<List<Disease>>(){}.getType());
	final  ArrayList<Disease> diseaselist=diseaseListData;
	runOnUiThread(new Runnable() {
		
		@Override
		public void run() {
			hideProgress();
			adapter=new DiseaseAdapter(ctx, diseaselist, uiHandler);
			diseaseList.setAdapter(adapter);
		}
	});
	
}
}
